 package datastructure;/*
 *  @类名: LinkStack
 *  @作者:梁恩铭、刘怡帆、陈敏、张吕炳
 *  @班级：2023网络空间安全本科一班
 *  @时间:2024.10.31
 *  @描述:通过链表形式实现栈结构，链表中的节点类可利用可复用LinkNode

 */

public class LinkStack<E> implements Stack<E> {
    //张吕炳
    LinkNode<E> head; 
    class LinkNode<E> {
        E data;
        LinkNode<E> next;

        LinkNode(E data) {
            this.data = data;
            this.next = null;
        }
    }
    
    //梁恩铭
    public LinkStack() {
        this.head = null;
    }
 
    @Override
    public boolean empty() {
        return head == null;
    }
 
    @Override
    public void push(E e) {
        LinkNode<E> newNode = new LinkNode<>(e);
        if (head == null) {
            head = newNode;
        } else {
            newNode.next = head;
            head = newNode;
        }
    }
    //陈敏
    @Override
    public E pop() {
        if (empty()) {
            return null;
        }
        E data = head.data;
        head = head.next;
        return data;
    }
 
    @Override
    public E peek() {
        if (empty()) {
            return null;
        }
        return head.data;
    }
}